package sjtu.ist.warehouse4ws.tool;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Map;

import javax.swing.text.View;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;

import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;
import sjtu.ist.warehouse4ws.model.ShiroUser;
import sjtu.ist.warehouse4ws.model.file.CompanyFileManager;
import sjtu.ist.warehouse4ws.model.file.CompanyFileManagerImplement;
import sjtu.ist.warehouse4ws.model.file.FileFolder;
import sjtu.ist.warehouse4ws.model.file.FileFolderImp;
import sjtu.ist.warehouse4ws.model.file.Suit;
import sjtu.ist.warehouse4ws.model.file.SuitImplement;
import sjtu.ist.warehouse4ws.model.file.Version;
import sjtu.ist.warehouse4ws.model.file.VersionImp;
import sjtu.ist.warehouse4ws.model.file.ViewImplement;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShowDomainModelXmlAction extends ActionSupport {
	
	String nodeName=null;
	String companyName=null;
	
	String suitXmlJson=null;
	String suitContentJson=null;
	String versionJson=null;
	String viewJson=null;
	String fileFolderJson =null;
	
	String modelXmlJson=null;
	String epcXmlJson=null;
	String functionXmlJson=null;
	String processXmlJson=null;
	String departmentXmlJson=null;
	String wadlXmlJson=null;
	String bpmnXmlJson=null;
	String permissionXmlJson=null;
	String roleXmlJson=null;
	String wsdlXmlJson=null;
	String bpelXmlJson=null;
	RepositoryMoudl repositoryMoudlService;

	public void setRepositoryMoudlService(RepositoryMoudl repositoryMoudlService) {
		this.repositoryMoudlService = repositoryMoudlService;
	}
	ArrayList<String> list=new ArrayList<String>();

//显示那几套
	public String showModelXml(){
		Subject currentUser = SecurityUtils.getSubject();

		String user_company =currentUser.getSession().getAttribute("companyId").toString();
		companyName=user_company;
		System.out.println(user_company);
		//MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/"));
		CompanyFileManager cfm=CompanyFileManagerImplement.getInstance(companyName,repositoryMoudlService);
		//System.out.println(cfm.getPath());
		
		
		suitXmlJson=showTreeList(cfm.getSuits());
		return SUCCESS;
	}
//显示模型的版本层
	public String showSuitContent() throws UnsupportedEncodingException
	{
		nodeName=java.net.URLDecoder.decode(nodeName,"UTF-8");
		Suit suit=new SuitImplement(nodeName);
		ArrayList<Version> versions=suit.getVersions(repositoryMoudlService);

		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		for(Version version:versions){
			processjson.append("{text:'");
			processjson.append(version.getName());
			processjson.append("'");
			processjson.append(",leaf:false,");
			processjson.append("loader:new Ext.tree.JsonPluginTreeLoader({dataUrl:");
			processjson.append("'showVersionContent?nodeName="+version.getPath()+"'})");
			//processjson.append(",listener:{'click' : function(node) {var i=n.parentNode.id}");
			processjson.append("},");
			
			
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
	
		suitContentJson=processjson.toString();
		
		return SUCCESS;
	}
	//显示每个版本下的那些文件和文件夹           project这里改
	public String showVersionContent(){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		Version version=new VersionImp(nodeName);
		ArrayList<sjtu.ist.warehouse4ws.model.file.View> views=version.getViews(repositoryMoudlService);
		for(sjtu.ist.warehouse4ws.model.file.View view:views){
		processjson.append(showNode(view.getName(),view.getPath(),view.getPath()));
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
		versionJson=processjson.toString();
		return SUCCESS;
	}
	//显示视图下的内容             改视图文件判断
	public String showViewContent(){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		sjtu.ist.warehouse4ws.model.file.View view=new ViewImplement(nodeName);
		ArrayList<FileFolder> fileFolders=view.getFileFolders(repositoryMoudlService);
		for(FileFolder fileFolder:fileFolders){
			if(fileFolder.getName().contains(".")){
				processjson.append("{id:'");
				processjson.append(fileFolder.getPath());
				processjson.append("',");
			processjson.append("text:'");
			processjson.append(fileFolder.getName());
			processjson.append("'");
			processjson.append(",leaf:true");
			processjson.append("},");
		}else{
			processjson.append("{id:'");
			processjson.append(fileFolder.getPath());
			processjson.append("',");
				processjson.append("text:'");
				processjson.append(fileFolder.getName());
				processjson.append("'");
				processjson.append(",leaf:false,");
				processjson.append("loader:new Ext.tree.JsonPluginTreeLoader({dataUrl:");
				processjson.append("'showFileFolderContent?nodeName="+fileFolder.getPath()+"'})");
				//processjson.append(",listener:{'click' : function(node) {var i=n.parentNode.id}");
				processjson.append("},");
			
		}
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
		viewJson=processjson.toString();
		return SUCCESS;
	}
	//最后一层文件夹内容
	public String showFileFolderContent(){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		FileFolder fileFolder=new FileFolderImp(nodeName);
		ArrayList<sjtu.ist.warehouse4ws.model.file.File> files=fileFolder.getFiles(repositoryMoudlService);
		for(sjtu.ist.warehouse4ws.model.file.File file:files){
			processjson.append("{id:'");
			processjson.append(file.getPath());
			processjson.append("',");
			processjson.append("text:'");
			processjson.append(file.getName());
			processjson.append("'");
			processjson.append(",leaf:true");
//			processjson.append("loader:new Ext.tree.JsonPluginTreeLoader({dataUrl:");
//			processjson.append("'showFileFolderContent?nodeName="+file.getPath()+"'})");
			//processjson.append(",listener:{'click' : function(node) {var i=n.parentNode.id}");
			processjson.append("},");
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
		fileFolderJson=processjson.toString();
		return SUCCESS;
	}
	public String showProcessXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/process"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/process/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		 processXmlJson=showFunction(list);
		return SUCCESS;
	}
	public String showDepartmentXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/department"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/department/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		 departmentXmlJson=showFunction(list);
		return SUCCESS;
	}
	
	public String showPermissionXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/permission"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/permission/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		 permissionXmlJson=showFunction(list);
		return SUCCESS;
	}
	
	public String showRoleXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/role"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/role/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		 roleXmlJson=showFunction(list);
		return SUCCESS;
	}
	
	
	public String showBpelXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/bpel"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/bpel/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		bpelXmlJson=showFunction(list);
		return SUCCESS;
	}
	public String showWsdlXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/wsdl"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/wsdl/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		wsdlXmlJson=showFunction(list);
		return SUCCESS;
	}
	
	public String showNode(String text,String id,String link){
		StringBuffer processjson = new StringBuffer();
		if(text.contains(".")){
			processjson.append(" {text : \"");
			processjson.append(text);
			processjson.append("\",id : '");
			processjson.append(id);
			processjson.append("',leaf:true");
			processjson.append("}, ");
		}else{
			processjson.append(" {text : \"");
			processjson.append(text);
			processjson.append("\",id : '");
			processjson.append(id);
			processjson.append("',leaf:false,loader : new Ext.tree.JsonPluginTreeLoader({dataUrl : '");
			processjson.append("showViewContent");
			processjson.append("?nodeName="+link+"'})}, ");
		}
	
		
		String str=processjson.toString();
		return str;
	}
	public String showModels(ArrayList<String> list){
		StringBuffer processJson=new StringBuffer();
		

		processJson.append("[");
		for(int i=0;i<list.size();i++){
			processJson.append("{");
			processJson.append("id:'");
			processJson.append(list.get(i));
			processJson.append("',");
			i++;
			processJson.append("text:'");
			processJson.append(list.get(i));
			processJson.append("',");
			processJson.append("leaf:true },");
			
		}
		processJson.deleteCharAt(processJson.length()-1);
		processJson.append("]");
		
		return processJson.toString();
	}
	public String showFunction(ArrayList<String> list){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		for(String function:list){
			processjson.append("{text:'");
			processjson.append(function);
			processjson.append("'");
			processjson.append(",leaf:true },");
			
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
	
		return processjson.toString();
	}
	public String showTreeList(ArrayList<Suit> suits)
	{
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		for(Suit suit:suits){
			processjson.append("{text:'");
			processjson.append(suit.getName());
			processjson.append("'");
			processjson.append(",leaf:false,");
			processjson.append("loader:new Ext.tree.JsonPluginTreeLoader({dataUrl:");
			processjson.append("'showSuitContent?nodeName='+"+"encodeURI(encodeURI('"+suit.getPath()+"'))"+"})");
			//processjson.append(",listener:{'click' : function(node) {var i=n.parentNode.id}");
			processjson.append("},");
			
			
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
	
		return processjson.toString();
	}
	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public String getSuitXmlJson() {
		return suitXmlJson;
	}

	public void setSuitXmlJson(String suitXmlJson) {
		this.suitXmlJson = suitXmlJson;
	}

	public String getSuitContentJson() {
		return suitContentJson;
	}

	public void setSuitContentJson(String suitContentJson) {
		this.suitContentJson = suitContentJson;
	}

	public String getVersionJson() {
		return versionJson;
	}

	public void setVersionJson(String versionJson) {
		this.versionJson = versionJson;
	}

	public String getViewJson() {
		return viewJson;
	}

	public void setViewJson(String viewJson) {
		this.viewJson = viewJson;
	}

	public String getFileFolderJson() {
		return fileFolderJson;
	}

	public void setFileFolderJson(String fileFolderJson) {
		this.fileFolderJson = fileFolderJson;
	}

	public String getModelXmlJson() {
		return modelXmlJson;
	}
	public void setModelXmlJson(String modelXmlJson) {
		this.modelXmlJson = modelXmlJson;
	}
	public ArrayList<String> getList() {
		return list;
	}
	public void setList(ArrayList<String> list) {
		this.list = list;
	}
	public String getEpcXmlJson() {
		return epcXmlJson;
	}
	public void setEpcXmlJson(String epcXmlJson) {
		this.epcXmlJson = epcXmlJson;
	}
	public String getFunctionXmlJson() {
		return functionXmlJson;
	}
	public void setFunctionXmlJson(String functionXmlJson) {
		this.functionXmlJson = functionXmlJson;
	}
	public String getProcessXmlJson() {
		return processXmlJson;
	}
	public void setProcessXmlJson(String processXmlJson) {
		this.processXmlJson = processXmlJson;
	}
	public String getDepartmentXmlJson() {
		return departmentXmlJson;
	}
	public void setDepartmentXmlJson(String departmentXmlJson) {
		this.departmentXmlJson = departmentXmlJson;
	}
	
	public String getPermissionXmlJson() {
		return permissionXmlJson;
	}
	public void setPermissionXmlJson(String permissionXmlJson) {
		this.permissionXmlJson = permissionXmlJson;
	}
	
	public String getRoleXmlJson() {
		return roleXmlJson;
	}
	public void setRoleXmlJson(String roleXmlJson) {
		this.roleXmlJson = roleXmlJson;
	}
	
	public String getWadlXmlJson() {
		return wadlXmlJson;
	}
	public void setWadlXmlJson(String wadlXmlJson) {
		this.wadlXmlJson = wadlXmlJson;
	}
	public String getBpmnXmlJson() {
		return bpmnXmlJson;
	}
	public void setBpmnXmlJson(String bpmnXmlJson) {
		this.bpmnXmlJson = bpmnXmlJson;
	}
	public String getWsdlXmlJson() {
		return wsdlXmlJson;
	}
	public void setWsdlXmlJson(String wsdlXmlJson) {
		this.wsdlXmlJson = wsdlXmlJson;
	}
	public String geBpelXmlJson() {
		return wsdlXmlJson;
	}
	public void setBpelXmlJson(String wsdlXmlJson) {
		this.wsdlXmlJson = wsdlXmlJson;
	}
	public String getBpelXmlJson() {
		return bpelXmlJson;
	}
	

	
}
